from time import sleep, strftime, localtime, time

from selenium.webdriver import ActionChains
from selenium.webdriver.common.by import By
from common.logger_util import write_loging ,error_loging
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium import webdriver
from common.dir_config import save_screenshot
from common.public_util import screenshot
from common.dir_config import file

class Library:

    def open_brower(self,browser):
        try:
            browser=browser.capitalize()
            self.drive=getattr(webdriver,browser)()
            write_loging(f'打开浏览器成功{browser}')
        except:
            self.save_screenshot()
            error_loging(f"打开{browser}浏览器失败")

    def load_url(self,url):
        try:
           self.drive.get(url)
           self.drive.implicitly_wait(10)
           write_loging(f'请求地址{url} 成功！')
        except:
            error_loging(f'请求地址{url}失败！')
            raise

    def locator(self,type,express):
        try:
            element=self.drive.find_element(type,express)
            write_loging(f'定位元素: {type,express} 成功!')
        except:
            error_loging(f"定位元素:{type,express} 失败！")
            self.save_screenshot()
            raise
        return element

    '关键函数输入'
    def input(self,type,express,value):
        try:
           self.WebDriverWait_visiblity(type, express)
           self.locator(type,express).send_keys(value)
           write_loging(f'定位{type,express}方法成功，输入信息{value}')
        except:
            error_loging(f"定位{type,express}方法失败！无法输入信息{value}")
            self.save_screenshot()
            raise Exception('输入异常')


    '关键字函数地点击'
    def click(self,type,express):
         try:
            self.WebDriverWait_visiblity(type, express)
            self.locator(type,express).click()
            write_loging(f'{type,express}方法定位成功，点击成功！')
         except:
             error_loging(f"{type,express}方法定位失败，点击失败！")
             raise Exception('点击异常')




    '获取某个元素的文本值'
    def assertText(self,type,express,expect):
        try:
            self.WebDriverWait_visiblity(type, express)
            el=self.locator(type,express)
            fact=el.text
            if fact!= expect:
                raise Exception(f'断言失败，assertText：fact！=expect')
            write_loging(f"断言：{fact}={express}流程成功")
        except:
            self.save_screenshot()
            error_loging(f'{express}文本断言流程失败')
            raise

    def sleep(self,s):
        try:
           sleep(s)
        except:
            error_loging('停留时间出现问题')
            raise

    def WebDriverWait_visiblity(self,type, express , timeout=5, poll=0.5):#显示等待，元素显示在dome树种。
        try:
            WebDriverWait(self.drive, timeout, poll).until(EC.visibility_of_element_located((type,express)))
            write_loging(f"在dome树中找到隐藏元素元素：{type, express }存在")
        except:
            error_loging(f"在dome树中未找到元素：{type, express }存在！！！")


    #封装方法，调用当前雷翔面的所有关键字参数
    def run(self,keyword,*args):
        '''该方法调用当前类的关键字'''
        getattr(self,keyword)(*args)


    def save_screenshot(self):
        st = strftime('%Y-%m-%d-%H:%M:%S', localtime(time()))
        fime_path = save_screenshot + '/' + st + '.png'
        self.drive.save_screenshot(fime_path)
        error_loging(f"失败截图，截取当前网页，存储的路径:{fime_path}")

    def switch_wiondow(self,value):
        try:
            wind=self.drive.window_handles
            self.drive.switch_to.window(wind[value])
            write_loging('切换窗口成功')
        except:
            error_loging('切换窗口失败')

    image=file +'/090.jpg'
    def gain_image(self,type, express,value=image):
        try:
            self.WebDriverWait_visiblity(type, express)
            self.locator(type, express).send_keys(f'{value}')
            write_loging(f'定位{type, express}方法成功，输入信息{value}')
        except:
            self.save_screenshot()
            error_loging(f"定位{type, express}方法失败！无法输入信息{value}")

    def title(self,value):#关键字title
            tile=self.drive.title
            if tile == value:
                write_loging(f'断言成功期望值:{value}等于实际值')
            else:
                error_loging(f'断言失败期望值:{tile}不等于实际值:{value}')
                self.save_screenshot()
                raise Exception('title不符合期望的值')

    def switch_allert(self,value):
        alert=self.drive.switch_to.alert.text
        if alert == value:
            write_loging(f'断言弹框期望值:【{value}】等于实际值【{alert}】')
        else:
            error_loging(f'断言失败期望值:{value}不等于实际值:{alert}')
            self.save_screenshot()
            raise Exception('alert弹框值不符合期望值')

    def switch_iframe(self,type, express): #进入iframe
        iframe=self.locator(type, express)
        self.drive.switch_to.frame(iframe)

    def back_iframe(self):#退出iframe
        self.drive.switch_to.default_content()

    def alert_accept(self):#进入弹框进行确认
        try:
            self.drive.switch_to.alert.accept()
            write_loging('进入【弹框】点击确认成功')
        except:
            error_loging('进入弹框失败')
            raise

    def ActionChains_move(self,type,express):
        try:
            element=self.locator(type,express)
            ActionChains(self.drive).move_to_element(element).perform()
            write_loging(f'移动鼠标到元素{type,express}成功')
        except:
            error_loging(f'移动鼠标到元素,定位元素{type,express}失败')
            self.save_screenshot()
            raise




if __name__ == '__main__':
 ll=Library()
 ll.run('back_iframe')
